package com.finchina.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 作者：黄赛赛 on 2015/9/30 14:55
 *
 * @version V1.0
 * @Description: ${todo}()
 */
public class SixModuleView extends View {

    Paint mPaint;
    float center_radius, module_radius, line_width, circle_arc_divider;
    int circleColor;
    int arc_finish_color;
    int arc_donging_color;
    int centerX, centerY;
    int circleRadius;
    int paddingLeft, paddingRight, paddingTop, paddingBottom,  maxPadding;
    float halfIntersectDegree;//小圆和大圆相交一半角度

    int finishStep = 2;//完成到第几步
    int pauseStep = 3;// 暂停在第几步
    RectF oval = new RectF();
    public SixModuleView(Context context) {
        this(context, null);
    }

    public SixModuleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SixModuleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.SixModuleView, defStyleAttr, 0);
       //获取半斤
        center_radius = ta.getDimensionPixelOffset(R.styleable.SixModuleView_center_radius,60);
        module_radius = ta.getDimensionPixelOffset(R.styleable.SixModuleView_module_radius,100);
        line_width = ta.getDimensionPixelOffset(R.styleable.SixModuleView_line_width, 2);
        circle_arc_divider = ta.getDimensionPixelOffset(R.styleable.SixModuleView_circle_arc_divider, 5);
        // 获取颜色
        circleColor = ta.getColor(R.styleable.SixModuleView_circle_color, Color.RED);
        arc_finish_color = ta.getColor(R.styleable.SixModuleView_circle_color, Color.BLUE);
        arc_donging_color = ta.getColor(R.styleable.SixModuleView_circle_color, Color.YELLOW);

        ta.recycle();


        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStrokeWidth(line_width);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);


        int width =0, height = 0;
        if (widthMode == MeasureSpec.EXACTLY){// match_parent , accurate
            width = widthSize;
        }else {

            width = widthSize;
        }


        if (heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else {
                height = Math.min(width, heightSize);
        }


        setMeasuredDimension(width,height);


        centerX = width/2;
        centerY = height /2;
         paddingLeft = getPaddingLeft();
         paddingRight = getPaddingRight();
         paddingTop = getPaddingTop();
         paddingBottom = getPaddingBottom();

         maxPadding = Math.max(Math.max(paddingLeft, paddingRight), Math.max(paddingTop, paddingBottom));


        circleRadius = (int) (Math.min(width, height)/2 -maxPadding - module_radius);

        halfIntersectDegree =  (float)Math.toDegrees(Math.asin((float)(module_radius + circle_arc_divider)/(float)(2*circleRadius) ));
        halfIntersectDegree = halfIntersectDegree*2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        setBackgroundColor(Color.WHITE);
        drawCircle(canvas);
//        drawModules(canvas);
    }

    void drawModules(Canvas canvas){

        canvas.save();
        mPaint.setColor(circleColor);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.translate(centerX, centerY);
        for (int i = 0, size = 6; i < size; i++) {
            canvas.drawCircle(circleRadius, 0, module_radius, mPaint);
            canvas.rotate(60);
        }
        canvas.restore();
    }

    void drawCircle(Canvas canvas) {
        canvas.save();

        canvas.translate(centerX, centerY);

        int arcDividerLineCount = 0;
        if (finishStep > 0){// 画出已完成的扇型
            mPaint.setColor(arc_finish_color);
            mPaint.setStyle(Paint.Style.FILL);
            oval.set(-circleRadius, -circleRadius, circleRadius, circleRadius);
            canvas.drawArc(oval, -90, finishStep * 60, true, mPaint);
            arcDividerLineCount = finishStep-1;
        }
        if (pauseStep > 0){//画出暂停的扇型
            oval.set(-circleRadius, -circleRadius, circleRadius, circleRadius);
            mPaint.setColor(arc_donging_color);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawArc(oval, -90 + (pauseStep - 1) * 60, 60, true, mPaint);
            arcDividerLineCount = pauseStep -1;
        }

//        if (finishStep > 0){//画出已完成扇型间间隔线
//            mPaint.setColor(Color.WHITE);
//            mPaint.setStyle(Paint.Style.FILL);
//            for (int i=0;i<finishStep ; i++){
//                double angrad = Math.toRadians( - 30+60 * i);
//                canvas.drawLine(0, 0, (float) Math.cos(angrad) * circleRadius, (float) Math.sin(angrad) * circleRadius, mPaint);
//            }
//        }
//
//        if (pauseStep == 6){//画出暂停的扇型间间隔线
//            mPaint.setColor(Color.WHITE);
//            mPaint.setStyle(Paint.Style.FILL);
//                double angrad = Math.toRadians( - 90);
//                canvas.drawLine(0, 0, (float) Math.cos(angrad) * circleRadius, (float) Math.sin(angrad) * circleRadius, mPaint);
//        }

        if (arcDividerLineCount > 0){
            oval.set(-circleRadius, -circleRadius, circleRadius, circleRadius);

            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.FILL);
            Path arcDivider = new Path();
            for (int i=0; i < arcDividerLineCount; i++){


//                arcDivider.reset();
                arcDivider.moveTo(0, 0);
                arcDivider.arcTo(oval, -30 + i * 60, 60);

            }
            arcDivider.close();
            canvas.drawPath(arcDivider, mPaint);

        }




        mPaint.setColor(circleColor);
        mPaint.setStyle(Paint.Style.STROKE);

//        Log.e("degress","degress =" + halfIntersectDegree);
        oval.set(-circleRadius, -circleRadius, circleRadius, circleRadius);
        for (int i = 0, size = 6; i < size; i++) {
            canvas.drawArc(oval,(float)halfIntersectDegree, 60.0f- (float)(2*halfIntersectDegree),false,mPaint );
            canvas.rotate(60);
        }

        oval.set(circleRadius - module_radius - circle_arc_divider, -module_radius - circle_arc_divider, circleRadius + module_radius + circle_arc_divider, module_radius + circle_arc_divider);


        for (int i = 0, size = 6; i < size; i++) {
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(circleRadius, 0, module_radius + circle_arc_divider,mPaint);
            mPaint.setColor(circleColor);
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawArc(oval, 90 + (halfIntersectDegree / 2.0f), 180 - halfIntersectDegree, false, mPaint);

            canvas.rotate(60);
        }
        canvas.restore();
    }




}
